/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.model.statement;


import java.util.List;

import dex.compiler.model.base.Place;
import dex.compiler.model.expression.Expression;


/**
 * A switch statement.
 */
public class SwitchStatement extends Statement {
	

	/**
	 * The case blocks.
	 */
	private List<SwitchCase> cases;
	
	
	/**
	 * The default statement.  To be executed if none of the cases 
	 * match the test.  May be null if there is no default handler
	 * for this switch.
	 */
	private Statement defaultStatement;

	
	/**
	 * The test whose value to compare against the case blocks.
	 */
	private Expression test;
	
	

	/**
	 * Constructs a new switch statement.
	 * 
	 * @param place  The place where the switch statement originated
	 * @param test   the test expression whose value to compare against the cases 
	 * @param cases  the cases to compare against the test value
	 * @param statement  the 
	 */
	public SwitchStatement(Place place, Expression test, List<SwitchCase> cases, Statement statement) {
		super(place);
		this.test = test;
		this.cases = cases;
		defaultStatement = statement;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder("switch ");
		sb.append(test).append(" {\n");
		for (SwitchCase c : cases) {
			sb.append(c);
		}
		if (defaultStatement != null) {
			sb.append("default\n");
			sb.append(defaultStatement);
		}
		sb.append("}\n");
		return sb.toString();
	}

	
	/**
	 * Returns the list of cases that will be tried against the test expression
	 * value.
	 * 
	 * @return the cases.  
	 */
	public List<SwitchCase> getCases() {
		return cases;
	}


	/**
	 * Returns the default statement.  This statement will be executed if 
	 * none of the cases match the test.  This might be null if the switch
	 * statement has no default case.
	 * 
	 * @return Returns the defaultStatement.
	 */
	public Statement getDefaultStatement() {
		return defaultStatement;
	}


	/**
	 * Returns the expression whose value will be tested against the cases.
	 * 
	 * @return  the test expression.
	 */
	public Expression getTest() {
		return test;
	}


	@Override
	public boolean terminates() {
		if (defaultStatement == null) {
			return false;
		}
		if (!defaultStatement.terminates()) {
			return false;
		}
		for (SwitchCase c: cases) {
			if (!c.getBody().terminates()) {
				return false;
			}
		}
		return true;
	}

}
